package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.dao.cargo.FactoryDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ContractProductServiceImpl implements ContractProductService {
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private FactoryDao factoryDao;

    @Override
    public void save(ContractProduct contractProduct) {
        double amount = contractProduct.getCnumber() * contractProduct.getPrice();
        contractProduct.setAmount(amount);
        contractProductDao.insertSelective(contractProduct);

        // 添加货物时，需要把货物小计金额及货物数量同步到合同中去
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        contract.setProNum(contract.getProNum() + 1);
        contract.setTotalAmount(contract.getTotalAmount() + amount);
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        Double productAmountOld = contractProductDao.selectByPrimaryKey(contractProduct.getId()).getAmount();
        double amount = contractProduct.getCnumber() * contractProduct.getPrice();
        contractProduct.setAmount(amount);
        contractProductDao.updateByPrimaryKeySelective(contractProduct);

        // 修改货物时，需要把货物小计金额同步到合同中去
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount() + amount - productAmountOld);
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {
        // 删除货物时，需要把货物小计金额同步到合同中去，同时删除货物下的附件
        ContractProduct product = contractProductDao.selectByPrimaryKey(id);
        Contract contract = contractDao.selectByPrimaryKey(product.getContractId());
        contract.setProNum(contract.getProNum() - 1);
        contract.setTotalAmount(contract.getTotalAmount() - product.getAmount());

        ExtCproductExample cproductExample = new ExtCproductExample();
        cproductExample.createCriteria().andContractProductIdEqualTo(product.getId());
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(cproductExample);
        for (ExtCproduct extCproduct : extCproducts) {
            extCproductDao.deleteByPrimaryKey(extCproduct.getId());
        }

        contract.setExtNum(contract.getExtNum() - extCproducts.size());

        contractDao.updateByPrimaryKeySelective(contract);

        contractProductDao.deleteByPrimaryKey(id);
    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ContractProductExample example, int page, int size) {
        PageHelper.startPage(page, size);
        List<ContractProduct> list = contractProductDao.selectByExample(example);
        return new PageInfo(list, 5);
    }

    @Override
    public void saveAll(List<ContractProduct> list) {
        double totalAmount = 0.0;
        for (ContractProduct contractProduct : list) {
            // 设置工厂ID
            FactoryExample factoryExample = new FactoryExample();
            factoryExample.createCriteria().andFactoryNameEqualTo(contractProduct.getFactoryName());
            List<Factory> factories = factoryDao.selectByExample(factoryExample);
            if (factories.size() > 0) {
                contractProduct.setFactoryId(factories.get(0).getId());
            }

            double amount = contractProduct.getCnumber() * contractProduct.getPrice();
            totalAmount += amount;
            contractProduct.setAmount(amount);
            contractProductDao.insertSelective(contractProduct);
        }

        String contractId = list.get(0).getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        contract.setTotalAmount(contract.getTotalAmount() + totalAmount);
        contract.setProNum(contract.getProNum() + list.size());
        contractDao.updateByPrimaryKeySelective(contract);
    }
}
